<?php


namespace app\system\controller;

class File extends AdminBase
{
	public function upload_image($cid = 0)
	{
		$options = ["cid" => $cid];
		$result = \app\system\service\FileService::upload_image($options);
		$this->response($result["code"], $result["msg"]);
	}
	public function get_image_list($type = "")
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\FileService::get_image_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function del_image()
	{
		if (!$this->request->post("ids")) {
			$this->response(\app\system\model\Code::fail, "缺少参数");
		}
		$ids = $this->request->post("ids");
		$options = ["ids" => $ids];
		$result = \app\system\service\FileService::del_image($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del_image_single()
	{
		if (!$this->request->post("id")) {
			$this->response(\app\system\model\Code::fail, "缺少参数");
		}
		$id = [$this->request->post("id")];
		$options = ["ids" => [$id]];
		$result = \app\system\service\FileService::del_image($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_classify_list()
	{
		$result = \app\system\service\FileService::get_classify_list();
		$this->response(\app\system\model\Code::success, "获取成功", $result);
	}
	public function add_classify()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["name" => "require", "pid" => "require", "sort" => "require|integer"], ["name.require" => "分类名称不能为空！", "sort.require" => "请填写排序", "sort.integer" => "请填写整数"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\FileService::add_classify($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_classify_info($id)
	{
		$options = ["id" => $id];
		$result = \app\system\service\FileService::get_classify_info($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del_classify()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require"], ["id.require" => "id 不能为空！"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\FileService::del_classify($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function edit_classify()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require", "name" => "require", "pid" => "require", "sort" => "require|integer"], ["name.require" => "分类名称不能为空！", "sort.require" => "请填写排序", "sort.integer" => "请填写整数"]);
		if (!$validate->check($options)) {
			$this->response(-1, $validate->getError());
		}
		$result = \app\system\service\FileService::edit_classify($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "编辑成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_classify_choose()
	{
		$result = \app\system\service\FileService::get_classify_choose();
		$this->response(\app\system\model\Code::success, "获取成功", $result);
	}
}